package com.example.meterialproject.activitys.transition.demo7

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.example.meterialproject.R
import com.example.meterialproject.exts.click
import com.google.android.material.transition.MaterialSharedAxis
import kotlinx.android.synthetic.main.fragment_sms.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

/**
 *
 * @ClassName: SMSFragment
 * @Author: android 超级兵
 * @CreateDate: 4/28/22$ 2:31 PM$
 * TODO 短信验证
 */
class SMSFragment : Fragment() {
    companion object {
        const val TYPE = "type"
        const val ACCOUNT = "account"
        const val PWD = "pwd"

        // 短信时间
        const val SMS_TIME = 10
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        return inflater.inflate(R.layout.fragment_sms, container, false)
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        arguments?.getInt(TYPE)?.also {
            enterTransition = createTransition(it)
        }

        extendedBtn.click {
            if (extendedBtn.isExtended) {
                extendedBtn.shrink()

                lifecycleScope.launch {
                    arguments?.getString(ACCOUNT)?.also { account ->
                        tv2.text = "我们已将短信发送至:"
                        account.indices.asFlow()
                            .transform {
                                emit(account[it])
                                delay(200)
                            }.collect { char ->
                                tv2.append("$char")
                            }
                    }
                }

                lifecycleScope.launch {
                    (0 until SMS_TIME).asFlow()
                        .transform {
                            emit(it)
                            delay(1000)
                        }
                        .collect {
                            tvSend.text = "${SMS_TIME - it}s后可以重新发送!"
                            if (it >= SMS_TIME - 1) {
                                extendedBtn.extend()
                                tvSend.text = ""
                            }
                        }
                }
            }
        }

        editPwdInout.addTextChangedListener {
            if (activity is TransitionDemo7Activity) {
                (activity as TransitionDemo7Activity).takeOverSMS(
                    it?.length == 4,
                    arguments?.getString(ACCOUNT) ?: "",
                    arguments?.getString(PWD) ?: "",
                    it.toString().trim()
                )
            }
        }

    }

    private fun createTransition(type: Int) = let {
        val transition = when (type) {
            1 -> {
                MaterialSharedAxis(MaterialSharedAxis.X, true)
            }
            2 -> {
                MaterialSharedAxis(MaterialSharedAxis.Y, true)
            }
            else -> {
                MaterialSharedAxis(MaterialSharedAxis.Z, true)
            }
        }

        transition.addTarget(R.id.parentAccountPsd)
        transition.addTarget(R.id.parentSMS)
        transition
    }
}